package com.uchedao.seller.ui.carlist;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.uchedao.ulibrary.fragment.CommonActivity;

/**
 * Fragment 父类
 *
 * @author zhengxizhen 2014/04/20
 */
public abstract class BaseFragment extends Fragment {
    /**
     * 当前Fragment顶级视图
     */
    public View mRoot;

    /**
     * 当前程序上下文
     */
    private Context mContext;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity().getApplicationContext();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mRoot = inflater.inflate(getViewLayoutId(), container, false);
        initView();
        initListener();
        initData();
        return mRoot;
    }

    /**
     * 加载布局文件资源ID
     */
    public abstract int getViewLayoutId();
    /**
     * 设置监听
     */
    public abstract void initListener();
    /**
     * 设置监听
     */
    public abstract void initData();

    /**
     * 初始化UI控件
     */
    public abstract void initView();

    @Override
    public View getView() {
        return mRoot;
    }

    protected Context getContext() {
        return mContext;
    }

    protected Context getApplicationContext() {
        return mContext;
    }

    /**
     * 查找ID
     */
    public View findViewById(int id) {
        return mRoot.findViewById(id);
    }

    /**
     * 提示
     */
    public void showToast(String msg) {
        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
    }

    /**
     * 提示
     */
    public void showToast(int resString) {
        if (isAdded()) {
            showToast(getResources().getString(resString));
        }
    }

    /**
     * 前往
     *
     * @param fragment 目标视图
     * @param canBack  是否可返回
     */
    public final void toFragment(Fragment fragment, boolean canBack) {
        ((CommonActivity) getActivity()).toFragment(fragment, canBack);
    }

    /**
     * 前往
     *
     * @param fragment 目标视图
     * @param canBack  是否可返回
     * @param anim     是否执行动画
     */
    public final void toFragment(Fragment fragment, boolean canBack,
                                 boolean anim) {
        ((CommonActivity) getActivity()).toFragment(fragment, canBack, anim);
    }

    public boolean onBackPress() {
        return false;
    }

    /**
     * 获取Dialog 展示状态
     */
    public boolean isProgressDialogShowing() {
        return ((CommonActivity) getActivity()).isProgressDialogShowing();
    }

    /**
     * 创建并展示Dialog
     */
    public void showProgressDialog(String msg) {
        ((CommonActivity) getActivity()).showProgressDialog(msg);
    }

    /**
     * 关闭Dialog
     */
    public void cancelProgressDialog() {
        ((CommonActivity) getActivity()).cancelProgressDialog();
    }


}
